package k.web.cityManage;

import com.alibaba.fastjson.JSON;
import k.HelperClazz;
import k.dao.base.EmpDao;
import k.dao.base.OrgDao;
import k.dto.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by sure on 2018/4/12.
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpController {
    @Autowired
    private EmpDao empDao;
    @Autowired
    private OrgDao orgDao;
    /**
     * 人员管理主页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/empIndex")
    public String index(Model model, HttpServletRequest request) {
        //查询所有人
        model.addAttribute("emp_id", HelperClazz.getEMP().getEmp_id());
        model.addAttribute("emp_name", HelperClazz.getEMP().getEmp_name());
        model.addAttribute("emps",empDao.findALL());
        model.addAttribute("orgs",orgDao.findAll());
        return "system_manage/personnel_manage";
    }

    /**
     * 新增emp
     * @param emp
     */
    @RequestMapping(value = "/insertEmp")
    public String insertEmp(Emp emp,HttpServletResponse response) {
        if(emp.getEmp_id() == null ){
            empDao.insertEmp(emp);
        }else{
            empDao.updateEmp(emp);
        }
        return "redirect:/emp/empIndex";
    }
    /**
     * 删除emp
     * @param empId
     */
    @RequestMapping(value = "/delEmp")
    public String delEmp(String empId,HttpServletResponse response) {
        empDao.deleteEmp(empId);
        return "redirect:/emp/empIndex";
    }

    /**
     * empId查询当前登陆人
     * @param empId
     * @param response
     */
    @RequestMapping(value = "/queryEmp")
    public void queryEmp(String empId,HttpServletResponse response) {
        HelperClazz.renderJson(response, JSON.toJSONString( empDao.getEmpById(empId)));
    }
}
